package 力扣日常刷题.木22二月.第01天206;

import 力扣日常刷题.TreeNode;

import java.util.*;

/**
 * @author 帅小伙
 * @date 2022/2/6
 * @description
 */
public class Demo08JZ从上到下打印二叉树2 {


    public List<List<Integer>> levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        List<List<Integer>> res = new ArrayList<>();
        if (root != null) queue.add(root);
        while (!queue.isEmpty()) {
            List<Integer> tmp = new ArrayList<>();

            for (int i = queue.size(); i > 0; i--) {
                TreeNode node = queue.poll();
                tmp.add(node.val);
                if(node.left!=null) queue.add(node.left);
                if(node.right!=null) queue.add(node.right);
            }
            if(res.size()%2==1) Collections.reverse(tmp);
            res.add(tmp);
        }
        return res;
    }

}
